<%-- 
    Document   : basket
    Created on : 15-nov.-2012, 16:12:54
    Author     : Anthares
--%>

<%@page session="false" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib tagdir="/WEB-INF/tags" prefix="jt" %>
<core:set var="pagename" scope="request" value="basket"/>
<fmt:setLocale value="${requestScope.locale}"/>
<fmt:setBundle basename="Languages.lang"/><jt:layout pageTitle="Basket">
    <jsp:body>
        <core:if test="${pageContext.request.getSession(false)==null || empty pageContext.request.getSession(false).getAttribute('basket').lines}">
            <p><fmt:message key="basket.empty"/></p>
        </core:if>
        <a href="<core:url value="/basket/clear"/>"><fmt:message key="basket.clear"/></a>
        <table id="basket-content">
            <thead><tr>
                    <td></td>
                    <td><fmt:message key="basket.head.article"/></td>
                    <td><fmt:message key="basket.head.amount"/></td>
                    <td><fmt:message key="basket.head.unitprice"/></td>
                    <td><fmt:message key="basket.head.subtotal"/></td>
                    <td><fmt:message key="basket.head.action"/></td>
                </tr></thead>
                <core:forEach items="${pageContext.request.getSession(false).getAttribute('basket').lines}" var="item">
                <tr>
                    <td><img src="<core:url value="${item.value.product.image}"/>"</td>
                    <td>${item.value.product.traduction.libelle}</td>
                    <td>
                        <form id="line:${item.value.key}" method="POST" action="<core:url value="/basket/edit"/>">
                            <input alt="${item.value.key}" type="text" value="${item.value.quantity}" name="quantity" data-validation-engine="validate[required,custom[integer],min[1]]"/>
                            <input type="hidden" value="${item.value.key}" name="prodId"/>
                        </form>
                    </td>
                    <td><fmt:formatNumber value="${item.value.product.reducedPrice}" type="currency"/></td>
                    <td><fmt:formatNumber value="${item.value.linePrice}" type="currency"/></td>
                    <td><a href="<core:url value="/basket/remove"/>?prodId=${item.value.key}"><fmt:message key="basket.remove"/></a></td>
                </tr>
            </core:forEach>
                <tfoot>
                    <tr><td colspan="5"><fmt:message key="basket.total"/> : <fmt:formatNumber value="${pageContext.request.getSession(false).getAttribute('basket').sum}" type="currency"/></td></tr>
                </tfoot>
        </table>
        <p><a class="button" href="<core:url value="/order"/>"><fmt:message key="basket.order"/></a></p>

        <script>
            $(document).ready(function(){
                $('#basket-content tr td form').each(function(){
                    $(this).validationEngine('attach');
                });
                $('#basket-content tr td input[text]').change(function(){
                    var key = $(this).attr('alt');
                    $('form .line:'+key).submit();
                });
            });
        </script>
    </jsp:body>
</jt:layout>
